<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="采购订单ID" prop="orderId">
                        <el-input v-model.number="dataForm.orderId" placeholder="采购订单ID"></el-input>
                </el-form-item>
                <el-form-item label="关联的需求申请ID" prop="requestId">
                        <el-input v-model.number="dataForm.requestId" placeholder="关联的需求申请ID"></el-input>
                </el-form-item>
                <el-form-item label="供应商ID" prop="supplierId">
                        <el-input v-model.number="dataForm.supplierId" placeholder="供应商ID"></el-input>
                </el-form-item>
                <el-form-item label="采购订单号" prop="orderNumber">
                        <el-input v-model="dataForm.orderNumber" placeholder="采购订单号"></el-input>
                </el-form-item>
                <el-form-item label="采购日期" prop="orderDate">
                        <el-date-picker
                                v-model="dataForm.orderDate"
                                type="datetime"
                                placeholder="采购日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="预计到货日期" prop="expectedDeliveryDate">
                        <el-date-picker
                                v-model="dataForm.expectedDeliveryDate"
                                type="datetime"
                                placeholder="预计到货日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="订单状态" prop="status">
                        <el-input v-model="dataForm.status" placeholder="订单状态"></el-input>
                </el-form-item>
                <el-form-item label="付款方式" prop="paymentMethod">
                        <el-input v-model="dataForm.paymentMethod" placeholder="付款方式"></el-input>
                </el-form-item>
                <el-form-item label="订单总金额" prop="totalAmount">
                        <el-input v-model="dataForm.totalAmount" placeholder="订单总金额"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            orderId: null,
            requestId: null,
            supplierId: null,
            orderNumber: null,
            orderDate: null,
            expectedDeliveryDate: null,
            status: null,
            paymentMethod: null,
            totalAmount: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            orderId: [
                        { required: true, message: '采购订单ID不能为空', trigger: 'blur' }
            ],
            requestId: [
                        { required: true, message: '关联的需求申请ID不能为空', trigger: 'blur' }
            ],
            supplierId: [
                        { required: true, message: '供应商ID不能为空', trigger: 'blur' }
            ],
            orderNumber: [
                        { required: true, message: '采购订单号不能为空', trigger: 'blur' }
            ],
            orderDate: [
                        { required: true, message: '采购日期不能为空', trigger: 'blur' }
            ],
            expectedDeliveryDate: [
                        { required: true, message: '预计到货日期不能为空', trigger: 'blur' }
            ],
            status: [
                        { required: true, message: '订单状态不能为空', trigger: 'blur' }
            ],
            paymentMethod: [
                        { required: true, message: '付款方式不能为空', trigger: 'blur' }
            ],
            totalAmount: [
                        { required: true, message: '订单总金额不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.orderId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.orderId = id || 0
            if (this.dataForm.orderId) {
              this.$http({
                url: this.$http.adornUrl(`/app/purchase_order/info/${this.dataForm.orderId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apppurchaseorder
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/purchase_order/${!this.dataForm.orderId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>